<template>
	<view class="j-l">
		<status-bar style="background-color: #999999;"></status-bar>
		<!-- 导航 -->
		<view class="j-l-nav">
			<view class="hxf hxf-zuobian" @click="back"></view>
			<view class="j-l-r" @click="toLogin">去登录</view>
		</view>
		<!-- 欢迎 -->
		<view class="j-l-welcome">注册</view>
		<!-- 表单 -->
		<view class="j-l-form">
			<view class="j-l-input">
				<input class="j-l-i" v-model="params.username" type="number" maxlength="11" placeholder="请输入手机号" />
			</view>
			<view class="j-l-input">
				<input class="j-l-i" v-model="params.code" type="number" maxlength="6" placeholder="验证码" />
				<u-button class="j-l-btn" shape="circle" :disabled="isCodeDis" @click="postMobileCode">{{codeBtnText}}</u-button>
			</view>
			<view class="j-l-input">
				<input class="j-l-i" v-model="params.password" type="password" maxlength="20" placeholder="请输入密码" />
			</view>
			<view class="j-l-input">
				<input class="j-l-i" v-model="params.inviteCode" type="number" maxlength="6" placeholder="邀请码" />
			</view>
			<u-button class="j-s-btn" shape="circle" @click="postRegisterUser">注册</u-button>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default{
		data() {
			return {
				isCodeDis: false,
				codeBtnText: '获取验证码',
				params:{
					username:'',
					password:'',
					inviteCode:'',
					keyval:'',
					code:''
				}
			}
		},
		onLoad(e) {
			if(e.inviteCode){
				this.params.inviteCode = e.inviteCode;
			}
		},
		methods:{
			// 立即注册
			postRegisterUser(){
				
				// 判断手机号是否为空
				if(this.$u.test.isEmpty(this.params.username)){
					this.$refs.uToast.show({
						title:'手机号不能为空'
					})
					return false;
				}
				
				// 判断手机号格式是否正确
				if(!this.$u.test.mobile(this.params.username)){
					this.$refs.uToast.show({
						title:'手机号格式不正确'
					})
					return false;
				}
				
				// 判断密码是否为空
				if(this.$u.test.isEmpty(this.params.password)){
					this.$refs.uToast.show({
						title:'密码不能为空'
					})
					return false;
				}
				
				if(!this.$u.test.rangeLength(this.params.password, [6, 20])){
					this.$refs.uToast.show({
						title:'密码长度为6-20位的字母和数字组成'
					})
					return false;
				}
				
				// 判断邀请码是否为空
				if(this.$u.test.isEmpty(this.params.inviteCode)){
					this.$refs.uToast.show({
						title:'邀请码不能为空'
					})
					return false;
				}
				
				// 判断验证码是否为空
				if(this.$u.test.isEmpty(this.params.code)){
					this.$refs.uToast.show({
						title:'验证码不能为空'
					})
					return false;
				}
				
				// 判断验证码唯一值是否为空
				if(this.$u.test.isEmpty(this.params.keyval)){
					this.$refs.uToast.show({
						title:'请重新发送短信验证码'
					})
					return false;
				}
				
				// console.log(this.params)
				// 调用注册
				this.$u.api
						.postRegisterUser(this.params)
						.then(res => {
							console.log(res);
							// 调用账号密码登录
							this.postLoginCheck()
						})
						.catch(err => {
							console.log(err);
						});
			},
			// 账号注册成功之后自动使用密码登录
			postLoginCheck(){
				this.$u.api
						.postLoginCheck(this.params)
						.then(res => {
							console.log(res);
							uni.navigateBack({
								delta: 2
							})
						})
						.catch(err => {
							console.log(err);
						});
			},
			// 获取短信验证码
			postMobileCode(){
				
				// 判断手机号是否为空
				if(this.$u.test.isEmpty(this.params.username)){
					this.$refs.uToast.show({
						title:'手机号不能为空'
					})
					return false;
				}
				
				// 判断手机号格式是否正确
				if(!this.$u.test.mobile(this.params.username)){
					this.$refs.uToast.show({
						title:'手机号格式不正确'
					})
					return false;
				}
				
				this.$u.api
						.postMobileCode({
							type: 1,
							mobile: this.params.username
						})
						.then(res => {
							console.log(res);
							this.params.keyval = res.data.keyval;
							// 发送成功之后触发验证码倒计时
							this.timeInverted();
						})
						.catch(err => {
							console.log(err);
						});
			},
			// 发送验证码倒计时
			timeInverted() {
				this.isCodeDis = true;
				let initial = 120;
				this.codeBtnText = `${initial}s`;
				let intercal = setInterval(() => {
					initial--;
					this.codeBtnText = `${initial}s`;
					if (initial == 0) {
						this.codeBtnText = '重新获取';
						this.isCodeDis = false;
						clearInterval(intercal);
					}
				}, 1000);
			
				this.$refs.uToast.show('发送成功');
			},
			toLogin(){
				uni.navigateBack();
			},
			back(){
				uni.navigateBack();
			}
		}
	}
</script>

<style scoped lang="scss">
	.j-l{
		
		.j-l-nav{
			display: flex;
			padding: 65rpx 50rpx;
			align-items: center;
			justify-content: space-between;
			
			.hxf-zuobian{
				color: #797979;
				font-size: 55rpx;
				margin-left: -20rpx;
			}
			
			.j-l-r{
				color: #ED810D;
				font-size: 30rpx;
			}
		}
		
		.j-l-welcome{
			color: #333333;
			font-size: 50rpx;
			padding: 51rpx 50rpx;
		}
		
		.j-l-form{
			padding: 0 50rpx;
			
			.j-l-input{
				position: relative;
				
				.j-l-i{
					padding: 50rpx 0;
					border-bottom: 1rpx solid #F1F1F1;
				}
				
				.j-l-btn{
					position: absolute;
					top: 40rpx;
					right: 0;
					width: 194rpx;
					height: 63rpx;
					color: #121212;
					font-size: 26rpx;
					background:linear-gradient(90deg,rgba(248,195,5,1),rgba(254,166,2,1));
					
					&::after{
						border: 0;
					}
				}
			}
			
			.j-s-btn{
				color: #121212;
				margin-top: 55rpx;
				background:linear-gradient(90deg,rgba(248,195,5,1),rgba(254,166,2,1));
				
				&::after{
					border: 0;
				}
			}
		}
	}
</style>
